package com.supermarket_project.service.impl;

import com.supermarket_project.common.R;
import com.supermarket_project.mapper.ActivityMapper;
import com.supermarket_project.pojo.Activity;
import com.supermarket_project.service.ActivityService;
import com.supermarket_project.utils.ToSqlDate;
import com.supermarket_project.views.ActivityShow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    ActivityMapper activityMapper;

    /**
     * 获取所有活动的信息
     *
     * @return
     */
    @Override
    public R getAllActivityInfo_s() {
        R r = new R();
        List<ActivityShow> result_open = null;
        List<ActivityShow> result_end = null;
        Map result = new HashMap<>();//不要初始化为空，此处是使用对象，不是赋值
        try {
            result_open = activityMapper.getActivityInfo_open();
            result_end = activityMapper.getActivityInfo_end();
            result.put("open", result_open);
            result.put("end", result_end);
            r.setCode(200);
            r.setMsg("查询所有活动信息成功");
            r.setData(result);
            return r;
        } catch (Exception e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统错误");
            r.setData(null);
            return r;
        }
    }

    @Override
    public R addOneActivity_s(Map activity_map) {
        Long type_id = -1l;
        R r = new R();
        Activity activity = new Activity();
        //根据选择的活动类型先获得活动类型id
        try {
            type_id = activityMapper.getTypeId(activity_map.get("type_name").toString());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        //活动id自增

        //设置活动名称
        activity.setActivity_name(activity_map.get("activity_name").toString());
        //设置活动开启时间
        activity.setOpen_time(ToSqlDate.StringToDate(activity_map.get("open_time").toString()));
        //设置活动关闭时间
        activity.setEnd_time(ToSqlDate.StringToDate(activity_map.get("end_time").toString()));
        //设置活动的备注
        activity.setComment(activity_map.get("comment").toString());
        //活动状态默认为1
        activity.setStatus(Integer.parseInt(activity_map.get("status").toString()));
        //设置活动的类型id
        activity.setAc_activity_type_id(type_id);
        //设置管理员id
        activity.setAc_staff_id(Long.parseLong(activity_map.get("staff_id").toString()));
        try {
            int result = activityMapper.addOneActivity(activity);
            System.out.println(result);
            if (result == 1) {
                r.setCode(200);
                r.setMsg("插入成功");
                r.setData(result);
            } else {
                r.setCode(500);
                r.setMsg("插入失败");
                r.setData(result);
            }
            return r;
        } catch (Exception e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统错误");
            r.setData(null);
            return r;
        }
    }

    /**
     * 更新活动的状态
     *
     * @param activity_id
     * @return
     */
    @Override
    public R updateActivityStatus_s(long activity_id) {
        R r = new R();
        try {
            int now_status = activityMapper.getNowActivityStatus(activity_id);
            if (now_status == 1) {
                now_status = 0;
            } else if (now_status == 0) {
                now_status = 1;
            }
            int result = activityMapper.updateActivityStatus(now_status, activity_id);
            if (result == 1) {
                r.setCode(200);
                r.setMsg("更新活动状态成功");
                r.setData(result);
            } else {
                r.setCode(500);
                r.setMsg("更新活动状态失败");
                r.setData(result);
            }
            return r;
        } catch (Exception e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统错误");
            r.setData(null);
            return r;
        }
    }

    /**
     * 删除活动
     * @param delete_info_map
     * @return
     */
    @Override
    public R deleteActivity_s(Map delete_info_map) {
        R r = new R();
        long activity_id = Long.parseLong(delete_info_map.get("activity_id").toString());
        try {
            int result = activityMapper.deleteActivity(activity_id);
            if (result == 1) {
                r.setCode(200);
                r.setMsg("删除活动成功");
                r.setData(result);
            } else {
                r.setCode(500);
                r.setMsg("删除失败");
                r.setData(result);
            }
            return r;
        } catch (Exception e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统错误");
            r.setData(null);
            return r;
        }
    }
}
